#include "key.h"

uint32_t button_press_time = 0;
uint32_t button_release_time = 0;

uint8_t get_key_status(void)
{
    if (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_RESET)
    {
        HAL_Delay(10);
        if (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_RESET)
        {
            button_press_time = HAL_GetTick(); // 记录按下时间
        }
    }

    while(HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_RESET);  //等待松开按键
    
    HAL_Delay(10);
    if (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_SET)
    {
        button_release_time = HAL_GetTick(); // 记录松开时间
    }
    
    uint32_t press_duration = button_release_time - button_press_time; // 得到按键按下时间

    if (press_duration > 0 && press_duration < 1000)
    {
        return key_short_press;
    }else
    {
        return key_long_press;
    }
}

void key_init(void)
{
}

